package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.service.PasssqServiceljc;
import com.zr.serviceImpl.PasssqServiceImplljc;
import com.zr.util.JDBCUtil;
//同意申请控制器
public class PasssqControllerljc extends HttpServlet{
	PasssqServiceljc p = new PasssqServiceImplljc();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		PrintWriter pw = resp.getWriter();
		boolean flag = true;
		int sid = Integer.parseInt(req.getParameter("sid"));
		String sname = p.findStuname(sid);
		String tname = req.getParameter("tname");	
		String nr = req.getParameter("nr");
		String sqsj = req.getParameter("sj");	
		String score = req.getParameter("score");
		String cname = req.getParameter("cname");
		int cid = p.findCidByCname(cname);		
		String zt = "已同意申请";
		int id = Integer.parseInt(req.getParameter("id"));
		Date d = new Date();
		SimpleDateFormat sim = new SimpleDateFormat("YYYY年MM月dd日hh时mm分");
		String newsj = sim.format(d);
		Connection con = JDBCUtil.getConnection();
		try {
			con.setAutoCommit(false);
			p.insertBlog(sid, tname, cname, score, zt, sqsj, newsj, nr, con);
			p.insertScore(sid, cid, score, con);
			p.insertTblog(sid, tname, sname, cname, score, zt, sqsj, newsj, nr, con);
			p.deleteSQ(id, con);
			con.commit();
		} catch (SQLException e) {
			try {
				flag = false;
				con.rollback(); //回滚
			} catch (SQLException e1){
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		if(flag){
			pw.write("1");
		}else{
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}
	
}
